<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>13. 罗马数字转整数</title>
  </head>
  <body>
    <!-- start -->
    <div>https://leetcode.cn/problems/roman-to-integer/description/</div>
    <!-- end -->
  </body>
  <!-- js start -->
  <script type="text/javascript">
    const MAP_VAL = {
      I: 1,
      IV: 4,
      V: 5,
      IX: 9,
      X: 10,
      XL: 40,
      L: 50,
      XC: 90,
      C: 100,
      CD: 400,
      D: 500,
      CM: 900,
      M: 1000,
    };

    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function (s) {
      let number = 0;
      let cacheStr = s;

      const chartArr = s.split('');

      chartArr.map((char, index) => {
        if (char === undefined) return;
        debugger
        const nextChar = chartArr[index + 1];

        // 先试合并内容
        const testStr = `${char}${nextChar}`;
        if (MAP_VAL[testStr]) {
          number += MAP_VAL[testStr];
          chartArr[index + 1] = undefined;
        } else {
          number += MAP_VAL[char];
        }
      });

      return number;
    };

    console.log(romanToInt('MCMXCIV'));
  </script>
</html>
